<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head>
  <title>Ubiquity Web Search Commands for Japanese, nora - jamazon</title>
</head>
<body>
<p>This xhtml source contains commands for searching the Web.  Its
corresponding feed is <a href="ubiquity-commands-ja-nora-jamazon.html">here</a>.</p>

<div id="jamazon" style="display: none;">
    <style>
        .price span{color:#FF9999;}
        .timeLeft{color:#99FF99;font-weight:bold;}
        .deliveryDate{font-weight:bold;}
    </style>
    検索語:<b>${query}</b>
    <div class="items" id="items">
        {for item in items}
            <div class="item" id="item${item.key}" style="clear: both; padding: 0px; margin: 2px 0px 0px 0px;border:1px solid #777777; -moz-border-radius:4px;background:#333333;">
                <div style="border:1px solid #777777; -moz-border-radius:4px;background:#666666;margin:0px;padding:0px;font-size:85%;">
                    <a href="${item.url}" accesskey="${item.key}">
                    <span style="font-size:xx-small; border-width:1px; border-style:solid;-moz-border-radius:2px; padding:0px 2px 0px 2px; vertical-align:center; margin-left:1px; max-width:20px; max-height:10px; overflow:visible;float:none;background: #dddddd;color:#333333;">${item.key}</span><b>${item.title}</b>
                    </a>
                </div>
                <div style="float:left;">
                    {if item.image}
                        <img
                            src="${item.image.src}"
                            style="float: left; margin: 2px 4px 2px 2px; height: ${item.image.height}px; width: ${item.image.width}px;"
                            />
                    {/if}
                </div>
                <div style="font-size:70%;">
                    {if item.binding}
                        ${item.binding} 
                    {/if}
                    {if item.price}
                        <br /><span class="price" style="margin-right:15px;font-weight:bold;">${item.price.retail}</span>
                        <span class="price" style="font-size:85%;">${item.price.used}</span>
                    {/if}
                    <span style="font-size:85%;">
                        {if item.track}
                            <br />${item.track}
                        {/if}
                        {if item.sss}
                            <br />${item.sss}
                        {/if}
                    </span>
                </div>
                <div style="clear:both;"> </div>
            </div>
        {/for}
    </div>
    <div class="bottom" style="position:absolute;bottom:11px;right:2px;height:13px;width:100%;padding:0px;margin:0px;background:rgba(40,40,40,0.7);">
        <div class="note" style="font-size:70%;clear:both;float:left;">※[Ctrl]+[Alt]+[番号] でジャンプ</div>
        <div class="navigation" style="font-size:70%;float:right;">
            <a id="prev" accesskey="8"><span style="font-size:xx-small; border-width:1px; border-style:solid;-moz-border-radius:2px; padding:0px 2px 1px 2px; max-width:30px; max-height:10px; overflow:visible;float:none;background: #dddddd;color:#000000;box-shadow:2px 2px 2px black;">&lt;Prev(<u>8</u>)</span></a>
            Page <span id="page">${page}</span>
            <a id="next" accesskey="9"><span style="font-size:xx-small; border-width:1px; border-style:solid;-moz-border-radius:2px; padding:0px 2px 1px 2px; max-width:30px; max-height:10px; overflow:visible;float:none;background: #dddddd;color:#000000;box-shadow:2px 2px 2px black;">Next(<u>9</u>)&gt;</span></a>
        </div>
    </div>
</div>

<script class="commands"><![CDATA[

//
// jamazon search
// www.amazon.co.jp
//

CmdUtils.CreateCommand({
    name: "jamazon",
    takes: {search: noun_arb_text},
    icon: "http://www.amazon.co.jp/favicon.ico",
    description: "<a href=\"http://www.amazon.co.jp\">Amazon.co.jp</a> 内を検索します",
    homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
    author: {name: "hokunarin", email: "hokunarin@gmail.com"},
    license: "MPL",
    
    execute: function(directObject, modifier){
        
        var urlString = "http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords={QUERY}";
        
        var encodedQuery = escape( Utils.convertFromUnicode("Shift-JIS", directObject.text) );
        
        urlString = urlString.replace(/%s|{QUERY}/g, encodedQuery);
        Utils.openUrlInBrowser(urlString); 
        
    },
    
    preview: function(previewBlock, directObject) {
        
        if(!directObject.text || directObject.text.length < 1) {
            previewBlock.innerHTML = "Amazon.co.jp 内の主な商品を検索します";
            return;
        }
        
        previewBlock.innerHTML = "<b>" + directObject.summary + "</b> を Amazon.co.jp 内で検索";
        jQuery(previewBlock).append('<div style="position:absolute;top:40%;left:40%;width:100px;height:50px;" id="progressimage"><img src="http://ubiquity-commands-ja-nora.googlecode.com/files/image_7990.gif" /></div>').find("#progressimage").hide().fadeIn(2000);
        
        var apiUrl = "http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords={QUERY}";
        var encodedQuery = escape( Utils.convertFromUnicode("Shift-JIS", directObject.text) );
        apiUrl = apiUrl.replace(/%s|{QUERY}/g, encodedQuery);
        
        var ubiquityVersion = Application.prefs.get("extensions.ubiquity.lastversion").value;
        
        var lastCursor = 0;
        var currentPage = 1;
        var prevUrl = "";
        var currentUrl = "";
        var nextUrl = "";
        
        renderItems(0);
        
        function renderItems(pageDiff){
            
            jQuery("#items", previewBlock).replaceWith('<div style="position:absolute;top:40%;left:40%;width:100px;height:50px;" id="progressimage"><img src="http://ubiquity-commands-ja-nora.googlecode.com/files/image_7990.gif" /></div>').find("#progressimage").hide().fadeIn(2000);
            
            var itemsPerQuery = 16;
            var itemsPerPage = 5;
            //var currentPage = eval(jQuery("#page", previewBlock).text());
            
            if(currentPage>0){
                currentPage = currentPage + pageDiff;
            }else{
                currentPage = 1;
            }
            
            var queryUrlFirst = "";
            var queryUrlSecond = "";
            var startCursor = 1;
            
            if( pageDiff == 0 ){
                queryUrlFirst = apiUrl;
                queryUrlSecond = null;
                startCursor = 1;
            }else if( pageDiff > 0 ){
                if(lastCursor + itemsPerPage <= itemsPerQuery){
                    queryUrlFirst = currentUrl;
                    queryUrlSecond = null;
                    startCursor = lastCursor + 1;
                }else if(lastCursor == itemsPerQuery){
                    queryUrlFirst = nextUrl;
                    queryUrlSecond = null;
                    startCursor = 1;
                }else{
                    queryUrlFirst = currentUrl;
                    queryUrlSecond = nextUrl;
                    startCursor = lastCursor + 1;
                }
            }else{
                if(lastCursor - itemsPerPage >= itemsPerPage){
                    queryUrlFirst = currentUrl;
                    queryUrlSecond = null;
                    startCursor = lastCursor - itemsPerPage * 2 + 1;
                }else if(lastCursor <= itemsPerPage){
                    queryUrlFirst = prevUrl;
                    queryUrlSecond = null;
                    startCursor = itemsPerQuery + lastCursor - itemsPerPage * 2 + 1;
                }else{
                    queryUrlFirst = prevUrl;
                    queryUrlSecond = currentUrl;
                    startCursor = itemsPerQuery + lastCursor - itemsPerPage * 2 + 1;
                }
            }
            
            var items = [];
            var i = 0;
            
            /*
            var responseDataFirst = null;
            var responseDataSecond = null;
            */
            
            //first query
            CmdUtils.previewAjax(previewBlock, {
                type: "GET",
                url: queryUrlFirst,
                dataType: "html",
                //async: false,
                error: function() {
                    previewBlock.innerHTML = "Error searching Amazon.";
                },
                success: function(responseData) {
                    
                    responseData = jQuery(responseData);
                    
                    var endCursor = (startCursor + itemsPerPage - 1 ) <= itemsPerQuery ? (startCursor + itemsPerPage - 1 ) : itemsPerQuery;
                    
                    //responseDataFirst = responseData.find(".result:not(.fkmrSeeMoreMessage)").slice( startCursor - 1, endCursor );
                    
                    
                    responseData.find(".result:not(.fkmrSeeMoreMessage)").slice( startCursor - 1, endCursor).each(function(){
                        
                        var newItem = getNewItem( jQuery(this) );
                        
                        newItem.key = ++i;
                        
                        items.push(newItem);
                    });
                    
                    
                    lastCursor = endCursor;
                    currentUrl = queryUrlFirst;
                    prevUrl = responseData.find(".pagnPrev a").attr("href");
                    nextUrl = responseData.find(".pagnNext a").attr("href");
                    
                    var itemsFirst = i;
                    
                    if(queryUrlSecond){
                        
                        //second query
                        CmdUtils.previewAjax(previewBlock, {
                            type: "GET",
                            url: queryUrlSecond,
                            dataType: "html",
                            //async: false,
                            error: function() {
                                previewBlock.innerHTML = "Error searching Amazon.";
                            },
                            success: function(responseData) {
                                
                                responseData = jQuery(responseData);
                                
                                //responseDataSecond = responseData.find(".result:not(.fkmrSeeMoreMessage)").slice( 0, itemsPerPage - itemsFirst );
                                
                                responseData.find(".result:not(.fkmrSeeMoreMessage)").slice( 0, itemsPerPage - itemsFirst).each(function(){
                                    
                                    var newItem = getNewItem( jQuery(this) );
                                    
                                    newItem.key = ++i;
                                    
                                    items.push(newItem);
                                });
                                
                                
                                lastCursor = itemsPerPage - itemsFirst;
                                currentUrl = queryUrlSecond;
                                prevUrl = responseData.find(".pagnPrev a").attr("href");
                                nextUrl = responseData.find(".pagnNext a").attr("href");
                                
                                endAjax();
                            },
                            complete: function(){
                                //endAjax();
                            }
                            
                        });
                    
                    }else{
                        endAjax();
                    }
                    
                    
                }
                
            });
            
            function endAjax(){
                
                /*
                if(responseDataFirst){
                    jQuery(responseDataFirst).each(function(){
                        var itemDetails = jQuery(this);
                        var newItem = getNewItem(itemDetails);
                        newItem.key = ++i;
                        items.push(newItem);
                    });
                }
                
                if(responseDataSecond){
                    jQuery(responseDataSecond).each(function(){
                        var itemDetails = jQuery(this);
                        var newItem = getNewItem(itemDetails);
                        newItem.key = ++i;
                        items.push(newItem);
                    });
                }
                */
                
                if(i < 5){
                    lastCursor = i;
                    nextUrl = null;
                }
                
                if( currentPage == 1 ){
                    prevUrl = null;
                }
                
                
                var previewData = {
                    query: directObject.text,
                    items: items,
                    page: currentPage
                };
                
                previewBlock.innerHTML = CmdUtils.renderTemplate( jQuery("#jamazon", feed.dom).html(), previewData);
                
                if(currentPage > 1){
                    jQuery("#prev", previewBlock).show();
                    jQuery("#prev", previewBlock).click(function(){
                        renderItems( -1 );
                        jQuery(this).unbind();
                    });
                }else{
                    jQuery("#prev", previewBlock).hide();
                }
                
                if( nextUrl != null ){
                    jQuery("#next", previewBlock).show();
                    jQuery("#next", previewBlock).click(function(evt){
                        renderItems( 1 );
                        jQuery(this).unbind();
                    });
                }else{
                    jQuery("#next", previewBlock).hide();
                }
                
                //for Ubiquity 0.1.x / 0.5  difference
                if (ubiquityVersion.indexOf("0.1.") > -1){
                    jQuery("#prev", previewBlock).attr({href:""});
                    jQuery("#next", previewBlock).attr({href:""});
                }else{
                    jQuery("#prev", previewBlock).removeAttr("href");
                    jQuery("#next", previewBlock).removeAttr("href");
                }
                
            }
            
            
            function getNewItem(itemDetails){
                
                var newItem = {
                    title: itemDetails.find(".productTitle a").text(),
                    url: itemDetails.find(".productTitle a").attr("href")
                };
                
                newItem.binding = "";
                itemDetails.find(".productTitle span").each(function(){
                    newItem.binding += jQuery(this).text();
                });
                
                var imgsrc = null;
                if(itemDetails.find(".productImage img").length > 0){
                    imgsrc = itemDetails.find(".productImage img").attr("src").replace(/SL160_AA115/g,"SL160_AA55");
                }else if(itemDetails.find(".productTitle img").length > 0){
                    imgsrc = itemDetails.find(".productTitle img").attr("src").replace(/SL160_AA115/g,"SL160_AA55");
                }
                newItem.image = {
                    src : imgsrc,
                    height : 55,
                    width : 55
                };
                
                newItem.price = {
                    retail : itemDetails.find(".newPrice").html(),
                    used : itemDetails.find(".usedPrice").html()
                };
                
                newItem.track = itemDetails.find(".fastTrack").html();
                
                newItem.sss = itemDetails.find(".sss").html();
                
                return newItem;
                
            }
            
        }
    
    }
});




]]></script>
</body>
</html>
